package com.letv.core.parser;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.letv.core.bean.TopicDetailInfoListBean;
import com.letv.core.bean.TopicDetailInfoListBean.TopicDetailItemBean;
import com.letv.core.bean.TopicSubject;

/**
 * 获取专题信息
 * 
 * @author zhuqiao
 *
 */
public class TopicInfoListParser extends LetvMobileParser<TopicDetailInfoListBean> {
    private static final String SUBJECT = "subject";

    private static final String ALBUMLIST = "albumList";

    private static final String VIDEOLIST = "videoList";

    @Override
    public TopicDetailInfoListBean parse(JSONObject data) throws Exception {
        if (data != null) {
            TopicDetailInfoListBean result = new TopicDetailInfoListBean();
            result.setList(new ArrayList<TopicDetailItemBean>());
            JSONArray jsonArray = null;
            boolean isAlbum = true;
            try {
                JSONObject jsonSubject = getJSONObject(data, SUBJECT);
                if (jsonSubject != null) {
                    TopicSubject subject = new TopicSubject();
                    subject.setName(getString(jsonSubject, "name"));
                    subject.setDescription(getString(jsonSubject, "description"));
                    subject.setPubName(getString(jsonSubject, "pubName"));
                    subject.setTag(getString(jsonSubject, "tag"));
                    subject.setType(getInt(jsonSubject, "type"));
                    subject.setCtime(getString(jsonSubject, "ctime"));
                    subject.setCid(getInt(jsonSubject, "cid"));
                    result.setSubject(subject);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (has(data, ALBUMLIST)) {
                isAlbum = true;
                jsonArray = getJSONArray(data, ALBUMLIST);
            } else if (has(data, VIDEOLIST)) {
                isAlbum = false;
                jsonArray = getJSONArray(data, VIDEOLIST);
            }

            if (jsonArray != null) {
                if (jsonArray != null && jsonArray.length() > 0) {
                    JSONObject jsonAlbumItem = null;
                    TopicDetailItemBean item = null;
                    int len = jsonArray.length();
                    for (int i = 0; i < len; i++) {
                        try {
                            jsonAlbumItem = jsonArray.getJSONObject(i);
                            if (isAlbum) {
                                item = getTopicDetailAlbumInfo(jsonAlbumItem);
                            } else {
                                item = getTopicDetailVideoInfo(jsonAlbumItem);
                            }
                            if (result != null) {
                                result.getList().add(item);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            return result;
        }
        return null;

    }

    /**
     * 返回专题专辑列表
     * 
     * @return
     */
    private TopicDetailItemBean getTopicDetailAlbumInfo(JSONObject data) {
        TopicDetailItemBean mTopicDetailInfoPlayerLibs = new TopicDetailItemBean();
        try {
            mTopicDetailInfoPlayerLibs.setId(getString(data, "aid"));
            mTopicDetailInfoPlayerLibs.setName(getString(data, "name"));
            mTopicDetailInfoPlayerLibs.setSubName(getString(data, "subname"));
            mTopicDetailInfoPlayerLibs.setType(getInt(data, "type"));
            // mTopicDetailInfoPlayerLibs.setDuration(getInt(data, "duration"));
            mTopicDetailInfoPlayerLibs.setPlay(getInt(data, "play"));
            mTopicDetailInfoPlayerLibs.setDownload(getInt(data, "download"));
            mTopicDetailInfoPlayerLibs.setJump(getInt(data, "jump"));
            mTopicDetailInfoPlayerLibs.setIsEnd(getInt(data, "isEnd"));
            mTopicDetailInfoPlayerLibs.setEpisode(getString(data, "episode"));
            mTopicDetailInfoPlayerLibs.setNowEpisode(getString(data, "nowEpisodes"));
            mTopicDetailInfoPlayerLibs.setAlbumOrVideoType(getString(data, "albumType"));
            // if (has(data, "picAll")) {
            // JSONObject picAllObj = getJSONObject(data, "picAll");
            // if (has(picAllObj, "120*90")) {
            // mTopicDetailInfoPlayerLibs.setPic120_90(getString(picAllObj, "120*90"));
            // }
            // if (has(picAllObj, "300*300")) {
            // mTopicDetailInfoPlayerLibs.setPic300_300(getString(picAllObj, "300*300"));
            // }
            // if (has(picAllObj, "400*300")) {
            // mTopicDetailInfoPlayerLibs.setPic400_300(getString(picAllObj, "400*300"));
            // }
            // }
            if (getInt(data, "play") == 1) {
                return mTopicDetailInfoPlayerLibs;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 返回专题视频列表
     * 
     * @return
     */
    private TopicDetailItemBean getTopicDetailVideoInfo(JSONObject data) {
        TopicDetailItemBean mTopicDetailInfoPlayerLibs = new TopicDetailItemBean();
        try {
            mTopicDetailInfoPlayerLibs.setId(getString(data, "id"));
            mTopicDetailInfoPlayerLibs.setPid(getString(data, "pid"));
            mTopicDetailInfoPlayerLibs.setName(getString(data, "nameCn"));
            mTopicDetailInfoPlayerLibs.setSubName(getString(data, "subTitle"));
            mTopicDetailInfoPlayerLibs.setType(getInt(data, "type"));
            mTopicDetailInfoPlayerLibs.setDuration(getInt(data, "duration"));
            mTopicDetailInfoPlayerLibs.setPlay(getInt(data, "play"));
            mTopicDetailInfoPlayerLibs.setDownload(getInt(data, "download"));
            mTopicDetailInfoPlayerLibs.setJump(getInt(data, "jump"));
            mTopicDetailInfoPlayerLibs.setAlbumOrVideoType(getString(data, "videoType"));
            if (has(data, "picAll")) {
                JSONObject picAllObj = getJSONObject(data, "picAll");
                if (has(picAllObj, "120*90")) {
                    mTopicDetailInfoPlayerLibs.setPic120_90(getString(picAllObj, "120*90"));
                }
                if (has(picAllObj, "300*300")) {
                    mTopicDetailInfoPlayerLibs.setPic300_300(getString(picAllObj, "300*300"));
                }
                if (has(picAllObj, "400*300")) {
                    mTopicDetailInfoPlayerLibs.setPic400_300(getString(picAllObj, "400*300"));
                }
            }
            if (getInt(data, "play") == 1) {
                return mTopicDetailInfoPlayerLibs;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
